package com.tbit.uqbike.service.business.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.OtaAccessConfig;
import com.tbit.uqbike.service.business.OtaAccessKeyService;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.core.OtaAccessConfigDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author yee_
 * @version 1.0
 * @date 2023/3/8 11:09
 */
@Service
public class OtaAccessKeyServiceImpl extends ServiceImpl<OtaAccessConfigDao, OtaAccessConfig> implements OtaAccessKeyService {

    @Resource
    private AccountDao accountDao;

    @Override
    public boolean save(OtaAccessConfig config) {
        Account account = accountDao.getByAccountId(config.getBrandId());
        if (account == null) {
            throw new BaseException("账户不存在,请重新选择");
        }
        if (!Objects.equals(1, account.getType())) {
            throw new BaseException("不是品牌账户,请重新选择");
        }
        config.setBrandName(account.getName());
        OtaAccessConfig existConfig = this.getById(config.getBrandId());
        if (existConfig != null) {
            throw new BaseException("账户已存在,请问重复添加");
        }
        return super.save(config);
    }

    @Override
    public boolean updateById(OtaAccessConfig config) {
        OtaAccessConfig existConfig = this.getById(config.getBrandId());
        if (existConfig == null) {
            throw new BaseException("记录不存在");
        }
        return super.updateById(config);
    }

}
